      SUBROUTINE RO1TAP(IER,IDREF,TMIN,X1,Y1,Z1,X2,Y2,Z2,IN)
C
C    THIS SUBROUTINE WAS MODIFIED ON JAN 12, 1970. THE FOLLOWING CHANGES
C    WERE MADE:
C      *ADAPTED TO ACCEPT EPHEM INPUT
C      *CONSTANTS HAVE BEEN CHANGED TO DODS COMPATIBLE
C      *COMPUTATION IS IN DOUBLE PRECISION
C      *DOUBLE PRECISION ENTRY POINTS HAVE BEEN ADDED
C      *DATA POINTS ARE ROUNDED TO WHOLE MINUTES
C
      IMPLICIT REAL*8(A-H,O-Z)
      REAL*4 TMIN,X1,Y1,Z1,X2,Y2,Z2,DATLAM,ALAMB,AXIS,E,AINC,AM0,WZERO,
     2  OMEGA0,DATEWD,SECS,AINBUF,SSIN
      REAL*4 SX,SY,SZ,SVVX,SVVY,SVVZ,SD,STD,STS,SECHK,SELTAT,STD2,STS2
      DIMENSION SX(200,1),SZ(200,1),SY(200,1),SVVX(200,1),SVVY(200,1),SV
     1VZ(200,1)
      EQUIVALENCE(X(1,1),SX(1,1)),(Y(1,1),SY(1,1)),(Z(1,1),SZ(1,1)),
     1(SVVX(1,1),VVX(1,1)),(SVVY(1,1),VVY(1,1)),(SVVZ(1,1),VVZ(1,1)),
     2(SD,D),(STD,TD),(STS,TS),(SECHK,ECHK),(SELTAT,DELTAT),(STD2,TD2),
     3(STS2,TS2),(AINBUF(1),EINBUF(1))
         DIMENSION  GAR(2,1)
      DIMENSION X(100,1),TDS(1),TSS(1),TDE(1)
      DIMENSION Y(100,1),TSE(1)
      DIMENSION Z(100,1),NBRDY(12),IUNTS(1),ALCO2(6)
      DIMENSION VVX(100,1),ISKIP(1),TDSAV(1),IEND(1)
      DIMENSION VVY(100,1),TSSAV(1),TDSAV2(1)
      DIMENSION VVZ(100,1),TSSAV2(1),AINBUF(200),DATIN(1),EINBUF(350)
      DATA ALCO2(1)/-120.0/,ALCO2(2)/24.0/,ALCO2(3)/-12.0/
      DATA ALCO2(4)/12.0/,ALCO2(5)/-24.0/,ALCO2(6)/120.0/
      DATA NBRDY(1)/0/,NBRDY(2)/31/,NBRDY(3)/59/,NBRDY(4)/90/
      DATA NBRDY(5)/120/,NBRDY(6)/151/,NBRDY(7)/181/,NBRDY(8)/212/
      DATA NBRDY(9)/243/,NBRDY(10)/273/,NBRDY(11)/304/,NBRDY(12)/334/
      DATA IEPHEM,EPHEM/0,'EPHEM'/
      DATA IUNTS(1)/65535/,ISUB/0/,ISKIP(1)/0/,TDS/0.0D0/
      DATA IEND/0/,INIT/1/,ITYR/-99/
C
C           CONSTANTS ARE COMPATIBLE WITH DODS
C
         DATA   ERAD,TCAN/6378.166D0,806.81242D0/
      DATA DULKM,SECDUT / 1.0E-4,864.0/
         NAMELIST /IR/ TDAY,ZSIN,TS,TS2,TD,TD2,T2,T3,IROCK
      GOTO 30
      ENTRY DRO1TA(IER,IDREF,TMIN1,XR,YR,ZR,XV,YV,ZV,IN)
      ENTRY ORB1TA(IER,IDREF,TMIN1,X1,Y1,Z1,X2,Y2,Z2,IN)
      GOTO 31
      ENTRY DTAPRE(IN,IER,DBTEWD,TSIN,XR,YR,ZR,XV,YV,ZV)
      GOTO 32
   30 CONTINUE
C CONVERT INPUT TIME TO DAY COUNT AND SECONDS
4     TMIN1=TMIN
  31  CONTINUE
      
      ADAY=0.D0
      IDAY = TMIN1/1440.D0
      DCONST=IDAY*1440.0D00
      TSIN=(TMIN1-DCONST)*60.D00
    1 IDAY=IDREF+IDAY-121
      J=2
      DO 5 I=1,42
      ISUB=0
      IDAY=IDAY-365
      IF (MOD(J,4)) 2,2,3
    2 IDAY=IDAY-1
      ISUB=1
    3 IF (IDAY.LE.0) GO TO 10
    5 J=J+1
   10 IYR=56+J
      J=1
      TDAY=IDAY+365+ISUB
      GO TO 100
C DAY COUNT OF YEAR
      ENTRY TAPRE (IN,IER,DATEWD,SSIN,X1,Y1,Z1,X2,Y2,Z2)
      TSIN=SSIN
      DBTEWD=DATEWD
C    DOUBLE PRECISION ENTRY FOR TAPRE -DTAPRE-
   32 CONTINUE    
      J=1
C CONVERT DATE WORD TO DAYS OF YEAR
      ADAY=0.0D0
      IYR=DBTEWD/10000.0D0
      IMON=DBTEWD/100.0D0-DFLOAT(IYR)*100.0D0
      IF (MOD(IYR,4).EQ.0) ISUB=1
      IF (IMON.LE.2) ISUB=0
      TDAY=NBRDY(IMON)+DBTEWD-DFLOAT(IYR*100+IMON)*100.0D0+ISUB
C CHECK TO SEE IF TITLE RECORD HAS BEEN READ - IF NOT SKIP
100   IF(IUNTS(1).EQ.65535.OR.IUNTS(1).EQ.IN)GO TO 101
      ISKIP(1)=0
      IUNTS(1)=IN
      ITYR=-99
      TDS(1)=0.
      REWIND IN
      GO TO 4
101   IF(ISKIP(J).NE.0)GO TO 150
      REWIND IN
      GAR(1,J)=0.D0
      GAR(2,J)=0.D0
      READ(IN,END=715,ERR=720)(SX(I,1),I=1,20)
      IF(X(1,1).EQ.EPHEM) IEPHEM=1
      IF(IEPHEM.EQ.1) READ(IN,ERR=720) EINBUF
      ISKIP(J)=1
      IF(IEPHEM .EQ. 0) GO TO 147
      GAR(1,J)=X(2,1)
      GAR(2,J)=X(3,1)
      DATIN(J)=X(4,1)
      TDE(J)=X(8,1)
      TSE(J)=X(9,1)
      GO TO 150
  147 GAR(1,J)=SX(2,1)
      GAR(2,J)=SX(3,1)
      DATIN(J)=SX(4,1)
      TDE(J)=SX(8,1)
      TSE(J)=SX(9,1)
      DO 148 I=1,100
      X(I,1)=0.0D0
      Y(I,1)=0.0D0
      Z(I,1)=0.0D0
      VVX(I,1)=0.0D0
      VVY(I,1)=0.0D0
      VVZ(I,1)=0.0D0
  148 CONTINUE
      ECHK=0
      D=0
      TD=0
      TS=0
      DELTAT=0
      TD2=0
      TS2=0
  150 IF(INIT.EQ.0)GO TO 151
      INIT=0
      TCER=TCAN/ERAD
      DUER=DULKM*ERAD
      DEST=DULKM*ERAD*SECDUT/TCAN
  151 XR=0.0D0
      YR=0.0D0
      ZR=0.0D0
      XV=0.0D0
      YV=0.0D0
      ZV=0.0D0
      X1=0.0D0
      Y1=0.0D0
      Z1=0.0D0
      X2=0.0D0
      Y2=0.0D0
      Z2=0.0D0
C     IROCK - CHECKS FOR READ-BACKSPACE LOOP DURING VECTOR SEARCH
         IROCK = 0
         IEND(J) = 0
      TD=TDSAV(J)
      TS=TSSAV(J)
      TD2=TDSAV2(J)
      TS2=TSSAV2(J)
      IF (TDS(J).NE.0) GO TO 155
      IF(IEPHEM.EQ.1) GO TO 802
      READ(IN,END=715,ERR=720)SD,STD,STS,SELTAT,SD,(SX(I,J),SY(I,J),SZ(I
     1,J),SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=1,100,2)
      READ(IN,END=715,ERR=720)SD,STD2,STS2,SD,SD,(SX(I,J),SY(I,J),SZ(I,J
     1),SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=101,200,2)
      GO TO 803
  802 CONTINUE
      READ(IN,END=715,ERR=720)D,TD,TS,DELTAT,(X(I,J),Y(I,J),Z(I,J),VVX(I
     1,J),VVY(I,J),VVZ(I,J),I=1,50)
      READ(IN,END=715,ERR=720)D,TD2,TS2,D,(X(I,J),Y(I,J),Z(I,J),VVX(I,J)
     1,VVY(I,J),VVZ(I,J),I=51,100)
  803 CONTINUE
      CALL ROUND(D,TD,TS,DELTAT)
      CALL ROUND(D,TD2,TS2,D)
      TDS(J)=TD
      TSS(J)=TS
      TINC=TS2-TS
      IF (TD.LT.TD2) TINC=TINC+86400.0D0
C CHECK TO SEE THAT REQUESTED TIME IS NOT BEFORE START OF TAPE
  155 IF (TDAY.GT.TDS(J)) GO TO 160
      IF (TDAY.LT.TDS(J))  GO TO 700
      IF(TSIN.LT.TSS(J)+2.0D0*DELTAT.AND.TSIN.NE.TSS(J).AND.TSIN.NE.TSS(
     1J)+DELTAT) GO TO 700
C TIME IS NOT BEFORE TAPE START TIME
  160 TS2S=TS2
      IF ( (TD .EQ. TD2 - 1.0D0) .OR. (TD .EQ. TD2 + 364.0D0) .OR. (TD .
     1EQ.  TD2 + 365.0D0)) TS2S = TS2 + 86400.0D0
      IF(TDAY.EQ.TD.AND.TSIN.GE.TS+3.0D0*DELTAT.AND.TSIN.LE.TS2S+3.0D0*
     1    DELTAT.OR.TDAY.EQ.TD+1.AND.TSIN+86400.0D0.LE.TS2S+3.0D0*DELTAT
     2)       GO TO 50
      IF(TD.EQ.365.+TDAY.OR.TD.EQ.364.+TDAY.AND.TSIN+86400.D0.LE.TS2S+
     $3.0D0*DELTAT)GO TO 163
      GO TO 162
163   ISKIP(J)=0
      ITYR=-99
      TDS(J)=0.
      REWIND IN
      GO TO 4
162   CONTINUE
      DO 99 I=1,3
      IF(TDAY.EQ.TD.AND.TSIN.EQ.(TS+(I-1)*DELTAT)) GO TO 50
  99  CONTINUE
C READ ANOTHER RECORD
      IF (TDAY.GT.TD)  GO TO 40
      IF(TDAY.EQ.TD.AND.TSIN.GE.TS2S+3.0D0*DELTAT) GO TO 40
         IDUM1 = ((TD-TDAY)*86400.0D0+TS-TSIN)/TINC
         IRCNBR = IDUM1+3
      IF((TD.EQ.GAR(1,J)).AND.(TS.EQ.GAR(2,J))) GO TO 730
      DO 161 I=1,IRCNBR
  161 BACKSPACE IN
         IROCK = IROCK+1
      IF(IEPHEM.EQ.1) GO TO 804
      READ(IN,END=715,ERR=720)SD,STD,STS,SD,SD,(SX(I,J),SY(I,J),SZ(I,J),
     1SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=1,100,2)
      GO TO 805
  804 CONTINUE
      READ(IN,END=715,ERR=720)D,TD,TS,D,(X(I,J),Y(I,J),Z(I,J),VVX(I,J),V
     1VY(I,J),VVZ(I,J),I=1,50)
  805 CONTINUE
         GO TO 43
C SAVE LAST INPUT ARRAY
   40 IF (IEND(J).EQ.255)  GO TO 710
      IF(IYR.GE.ITYR)GO TO 41
      ITYR=IYR
      ISKIP(J)=0
      TDS(J)=0.
      REWIND IN
      GO TO 4
41    DO 42 I=1,50
      II=I+50
      X(I,J)=X(II,J)
      Y(I,J)=Y(II,J)
      Z(I,J)=Z(II,J)
      VVX(I,J)=VVX(II,J)
      VVY(I,J)=VVY(II,J)
   42 VVZ(I,J)=VVZ(II,J)
      TD=TD2
      TS=TS2
   43    CONTINUE
      IF(IEPHEM.EQ.1) GO TO 806
      READ(IN,ERR=720,END=715)SECHK,STD2,STS2,SD,SD,(SX(I,J),SY(I,J),
     1SZ(I,J),SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=101,200,2)
      GO TO 807
  806 CONTINUE
      READ(IN,ERR=720,END=715)ECHK,TD2,TS2,D,(X(I,J),Y(I,J),Z(I,J),
     1VVX(I,J),VVY(I,J),VVZ(I,J),I=51,100)
  807 CONTINUE
      IF(ECHK.GT.9999998.D0) GO TO 45
      CALL ROUND(ECHK,TD2,TS2,D)
      CALL ROUND(D,TD,TS,D)
      IF (TDAY-TD.GE.ADAY) TDAY=TDAY-ADAY
         IF(IROCK.GT. 2)   GO TO 730
      GO TO 160
   45 DO 46 I=1,50
      IF(X(I,J).GT.9999998.D0)GO TO 47
   46 CONTINUE
   47 TS2=TS+DELTAT*DFLOAT(I-3)
      IEND(J)=255
      GO TO 160
C VECTOR IS IN THIS RECORD
   50 TS12=TS
      ITYR=IYR
      IF (TSIN.LT.TS) TS12=TS-86400.0D0
      N=(TSIN-TS12)/DELTAT
      AN=TS12+DFLOAT(N)*DELTAT
      IF (TSIN-AN.NE.0.0D0) GO TO 60
      N=N+1
   51 CONTINUE
      XR=X(N,J)
      YR=Y(N,J)
      ZR=Z(N,J)
      XV=VVX(N,J)
      YV=VVY(N,J)
      ZV=VVZ(N,J)
      IER=6
 1000 CONTINUE
      IF(IEPHEM.EQ.1) GO TO 810
      XR=XR/ERAD
      YR=YR/ERAD
      ZR=ZR/ERAD
      XV=XV*TCER
      YV=YV*TCER
      ZV=ZV*TCER
      GO TO 811
  810 CONTINUE
      XR=XR/DUER
      YR=YR/DUER
      ZR=ZR/DUER
      XV=XV/DEST
      YV=YV/DEST
      ZV=ZV/DEST
  811 CONTINUE
      TSSAV2(J)=TS2
      TDSAV2(J)=TD2
      TSSAV(J)=TS
      TDSAV(J)=TD
      X1=XR
      Y1=YR
      Z1=ZR
      X2=XV
      Y2=YV
      Z2=ZV
      RETURN
C NORMAL  RETURN
C INTERPOLATE FOR VECTORS
   60 CAPT=(TSIN-AN)/DELTAT
      N=N+1
      DO 65 I=1,6
      AL=1.0D0
      IF (I.NE.1) AL=AL*(CAPT+2.0D0)
      IF (I.NE.2) AL=AL*(CAPT+1.0D0)
      IF (I.NE.3) AL=AL*CAPT
      IF (I.NE.4) AL=AL*(CAPT-1.0D0)
      IF (I.NE.5) AL=AL*(CAPT-2.0D0)
      IF (I.NE.6) AL=AL*(CAPT-3.0D0)
      AL=AL/ALCO2(I)
      IAL=N-3+I
      IF(X(IAL,J).EQ.0.D0) GO TO 61
      XR=XR+AL*X(IAL,J)
   61 IF(Y(IAL,J).EQ.0.D0) GO TO 62
      YR=YR+AL*Y(IAL,J)
   62 IF(Z(IAL,J).EQ.0.D0) GO TO 63
      ZR=ZR+AL*Z(IAL,J)
   63 IF(VVX(IAL,J).EQ.0.D0) GO TO 64
      XV=XV+AL*VVX(IAL,J)
   64 IF(VVY(IAL,J).EQ.0.D0) GO TO 66
      YV=YV+AL*VVY(IAL,J)
   66 IF(VVZ(IAL,J).EQ.0.D0) GO TO 65
      ZV=ZV+AL*VVZ(IAL,J)
   65 CONTINUE
      IER=6
      GO TO 1000
 700  IYR2=DATIN(J)/10000.D0
      IF (IYR2.LT.IYR)  GO TO 701
      IER=1
      X1=0.0D0
      Y1=0.0D0
      Z1=0.0D0
      X2=0.0D0
      Y2=0.0D0
      Z2=0.0D0
      TSSAV2(J)=TS2
      TDSAV2(J)=TD2
      TSSAV(J)=TS
      TDSAV(J)=TD
      RETURN
701   IF(TDAY-TD.GE.0.0D0.OR.TD.LT.TDS(J)) GO TO 160
      I1=0
      IF (MOD(IYR2,4).EQ.0) I1=1
      ADAY=365+I1
      TDAY=TDAY+ADAY
      GO TO 160
  710 IF (TDAY.EQ.TD.AND.(TSIN.EQ.TS2+DELTAT
     1.OR.TSIN.EQ.TS2+2.0D0*DELTAT.OR.TSIN.EQ.TS2+3.0D0*DELTAT)) GO TO 5
     *0
      ZSIN = TSIN
         WRITE(6,711)
         WRITE(6,IR)
  711    FORMAT(1H0,' **** PROGRAM HAS READ PAST END OF TAPE',/'  CHECK
     1VALIDITY OF TIME AND IDREF **** ')
  715    DO 716  KK=1,3
         BACKSPACE IN
  716    CONTINUE
      IF(IEPHEM.EQ.1) GO TO 808
      READ(IN,END=715,ERR=720)SD,STD,STS,SD,SD,(SX(I,J),SY(I,J),SZ(I,J),
     1SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=1,100,2)
      READ(IN,END=715,ERR=720)SD,STD2,STS2,SD,SD,(SX(I,J),SY(I,J),SZ(I,J
     1),SVVX(I,J),SVVY(I,J),SVVZ(I,J),I=101,200,2)
      GO TO 809
  808 CONTINUE
      READ(IN,END=715,ERR=720)D,TD,TS,D,(X(I,J),Y(I,J),Z(I,J),VVX(I,J),
     1VVY(I,J),VVZ(I,J),I=1,50)
      READ(IN,END=715,ERR=720)D,TD2,TS2,D,(X(I,J),Y(I,J),Z(I,J),VVX(I,J)
     1,VVY(I,J),VVZ(I,J),I=51,100)
  809 CONTINUE
      CALL ROUND(D,TD2,TS2,D)
      CALL ROUND (D,TD,TS,D)
         IEND(J) = 0
      TSSAV2(J)=TS2
      TDSAV2(J)=TD2
      TSSAV(J)=TS
      TDSAV(J)=TD
      IER=2
      RETURN
720   IER = 3
      TSSAV2(J)=TS2
      TDSAV2(J)=TD2
      TSSAV(J)=TS
      TDSAV(J)=TD
      RETURN
  730 CONTINUE
         TDS(J) = 0.D0
         REWIND  IN
         ISKIP(J) = 0
      N=0
      T2=TS2+DELTAT
      T3=TS2+DELTAT*2.0D0
      IF(TSIN.EQ.TS2) N=51
      IF(TSIN.EQ.T2) N=52
      IF(TSIN.EQ.T3) N=53
      IF(N.GT.0) GO TO 51
         WRITE(6,731)
  731    FORMAT(1H0,' **** TAPE IS IN LOOP SEARCHING FOR VECTOR',/' CHEC
     KK TIME INPUTS TO INSURE THAT YOU ARE NOT TRYING TO READ BETWEEN TH
     LE 1ST 3 VECTORS OF RECORD 1')
      ZSIN = TSIN
         WRITE(6,IR)
         IER = 4
         RETURN
C TAPRE1 ENTRY - READ HEADER RECORD AND OBTAIN ORBITAL ELEMENTS
      ENTRY TAPRE1 (IN,DATLAM,ALAMB,AXIS,E,AINC,AM0,WZERO,OMEGA0,DATEWD,
     1SECS)
C ENTRY TAPRE2  ENTRY FOR DOUBLE PRECISION HEADER
      ENTRY TAPRE2(IN,DBTLAM,DLAMB,DAXIS,DE,DINC,DM0,DZERO,DMEGA0,DBTEWD
     1,DSEC)
      IF(IUNTS(1).EQ.65535.OR.IUNTS(1).EQ.IN)GO TO 515
      ISKIP(1)=0
      IUNTS(1)=IN
      ITYR=-99
      TDS(1)=0.0
      REWIND IN
      GO TO 4
515   J=1
      REWIND IN
      READ(IN,ERR=720)  AINBUF
      IF(EINBUF(1).NE.EPHEM)GO TO 516
      REWIND IN
      READ(IN,ERR=720)EINBUF
      IEPHEM=1
  516 TDS(J)=0.0D0
         IRCNBR = 0
      GAR(1,J)=0.D0
      GAR(2,J)=0.D0
      IF(IEPHEM.EQ.1) GO TO 812
      GAR(1,J)  = AINBUF(2)
      GAR(2,J)  = AINBUF(3)
      DATIN(J)=AINBUF(4)
      DLAMB=AINBUF(29)
      DAXIS=AINBUF(102)
      DE=AINBUF(103)
      DINC=AINBUF(117)
      DM0=AINBUF(114)
      DZERO=AINBUF(116)
      DMEGA0=AINBUF(118)
      DBTLAM=AINBUF(27)
      DBTEWD=AINBUF(191)*10000.0+AINBUF(192)*100.0+AINBUF(193)
      DSEC=AINBUF(194)*3600.0+AINBUF(195)*60.0+AINBUF(196)/1000.0
      TDE(J)=AINBUF(8)
      TSE(J)=AINBUF(9)
      GO TO 813
  812 CONTINUE
      GAR(1,J)=EINBUF(2)
      GAR(2,J)=EINBUF(3)
      DATIN(J)=EINBUF(4)
      DAXIS=EINBUF(50)/ERAD
      DE=EINBUF(51)
      DINC=EINBUF(52)
      DM0=EINBUF(55)
      DZERO=EINBUF(53)
      DMEGA0=EINBUF(54)
      DBTEWD=EINBUF(44)*10000.0D0+EINBUF(45)*100.0D0+EINBUF(46)
      DBTLAM=DBTEWD
      IF(DBTEWD.GT.EINBUF(4))DBTLAM=EINBUF(4)
      DSEC=EINBUF(47)*3600.0D0+EINBUF(48)*60.0D0+EINBUF(49)/1000.0D0
      TDE(J)=EINBUF(8)
      TSE(J)=EINBUF(9)
      DLAMB=EINBUF(197)
      READ(IN) EINBUF
  813 CONTINUE
      ALAMB=DLAMB
      AXIS=DAXIS
      E=DE
      AINC=DINC
      AM0=DM0
      WZERO=DZERO
      OMEGA0=DMEGA0
      DATEWD=DBTEWD
      DATLAM=DBTLAM
      SECS=DSEC
      ISKIP(J)=1
      RETURN
      END
